"""
---------------------------------------------------------------------
- File: glass.py
- Date: 2025/11/6
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 班级学生一对多关系API
---------------------------------------------------------------------
"""

from fastapi import APIRouter, Depends, Path, Query, HTTPException

# service
from app.service.service_glass import ServiceGlass

# pydantic models
from app.schemas.glass import GlassRead, GlassCreate, GlassUpdate

# response
from app.core.response import standard_response, ResponseBase, ResponseSuccess
# service
from app.core.dependency import get_service_dependency
# logger
from app.core.logs import glass_logger

glass_router = APIRouter(
    tags=["ob1-班级学生一对多关系"],
    prefix="/ob1",
    responses=standard_response,
)

ServiceGlass = get_service_dependency(ServiceGlass)


@glass_router.get(
    "/glass/{glass_id}/",
    summary="获取某个班级信息",
    response_model=ResponseBase[GlassRead],
    response_description="成功的响应"
)
async def get_glass(
        glass_id: int = Path(..., description="班级ID", ge=1),
        service_glass: ServiceGlass = Depends(),
):
    """
    `根据班级id --> 返回班级信息或None`
    """
    glass_logger.info('访问就读班级信息')
    db_glass = await service_glass.get(id=glass_id)
    if db_glass is None:
        raise HTTPException(status_code=404, detail="班级不存在")
    return ResponseSuccess(data=db_glass)


@glass_router.get("/glass/", summary="获取班级信息列表")
async def get_glasses(
        page: int = Query(1, ge=1, description="页码"),
        size: int = Query(3, le=100, description="每页数量"),
        service_glass: ServiceGlass = Depends(),
) -> ResponseBase[list[GlassRead] | None]:
    """
    `根据页码和每页数量 -->返回班级列表或None`
    """
    db_glasses = await service_glass.get_multi(skip=(page - 1) * size, limit=size)
    return ResponseSuccess(data=db_glasses)


@glass_router.post("/glass/", summary="创建一个班级", response_model=ResponseBase[GlassRead])
async def create_glass(
        glass_in: GlassCreate,
        service_glass: ServiceGlass = Depends(),
):
    """
    `创建一个班级 --> 返回创建成功后的班级信息`
    """
    db_glass = await service_glass.create(obj_in=glass_in)
    return ResponseSuccess(data=db_glass)


@glass_router.patch(
    "/glass/{glass_id}/",
    summary="更新某个班级信息",
    response_model=ResponseBase[GlassRead],
    response_description="成功的响应"
)
async def update_glass(
        glass_in: GlassUpdate,
        glass_id: int = Path(..., description="班级id", ge=1),
        service_glass: ServiceGlass = Depends(),

):
    """
    `更新某个班级信息 --> 返回更新成功后的班级信息`
    """
    db_glass = await service_glass.get(id=glass_id)
    if not db_glass:
        raise HTTPException(status_code=404, detail="班级不存在")
    updated_glass = await service_glass.update(db_obj=db_glass, obj_in=glass_in)
    return ResponseSuccess(data=updated_glass)


@glass_router.delete("/glass/{glass_id}/", summary="删除某个班级信息", response_model=ResponseBase[GlassRead])
async def delete_glass(
        glass_id: int = Path(..., description="班级id", ge=1),
        service_glass: ServiceGlass = Depends(),
):
    """
    `删除某个班级信息 --> 返回删除成功的班级信息`
    """
    db_glass = await service_glass.get(id=glass_id)
    if not db_glass:
        raise HTTPException(status_code=404, detail="班级不存在")
    db_glass = await service_glass.remove(id=glass_id)

    if db_glass is None:
        raise HTTPException(status_code=404, detail="班级不存在")
    return ResponseSuccess(data=db_glass)
